{extend name='public/base' /}
{block name='content'}
{include file='public/content_header' /}
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <!-- 表单头部 -->
                <div class="card-header">
                    <div class="btn-group">
                        <a class="btn flat btn-sm btn-default BackButton">
                            <i class="fas fa-arrow-left"></i>
                            返回
                        </a>
                    </div>
                </div>
                <!-- 表单 -->
                <form id="dataForm" class="form-horizontal dataForm" action="" method="post"
                      enctype="multipart/form-data">
                    <!-- 表单字段区域 -->
                    <div class="card-body">
                        <div class="form-group row">
                            <label for="name" class="col-sm-2 col-form-label">标题</label>
                            <div class="col-sm-10 col-md-4 formInputDiv">
                                <input id="name" name="name" value="{$data.name|default=''}" placeholder="请输入标题"
                                       type="text" class="form-control fieldText">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="user_id" class="col-sm-2 col-form-label">发布人</label>
                            <div class="col-sm-10 col-md-4 formInputDiv">
                                <select name="user_id" id="user_id" class="form-control select2bs4 fieldSelect"
                                        data-placeholder="请选择发布人">
                                    <option value=""></option>
                                    {foreach name='user_list' id='item'}
                                    <option value="{$item.id}" {if isset($data) && $data.user_id==$item.id}selected{/if}>
                                        {$item.username}
                                    </option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>
                        <script>
                            $('#user_id').select2({
                                theme: 'bootstrap4'
                            });
                        </script>
                        <div class="form-group row">
                            <label for="article_category_id" class="col-sm-2 col-form-label">所属分类</label>
                            <div class="col-sm-10 col-md-4 formInputDiv">
                                <select name="article_category_id" id="article_category_id"
                                        class="form-control select2bs4 fieldSelect" data-placeholder="请选择所属分类">
                                    {$article_category_list|raw|default=''}
                                </select>
                            </div>
                        </div>
                        <script>
                            $('#article_category_id').select2({
                                theme: 'bootstrap4'
                            });
                        </script>

                        <div class="form-group row">
                            <label for="tag" class="col-sm-2 col-form-label">标签</label>
                            <div class="col-sm-10 col-md-4 formInputDiv">
                                <select name="tag[]" id="tag" data-placeholder="请选择标签"
                                        class="form-control fieldMultiSelect" multiple="multiple">
                                    <option value=""></option>
                                    {foreach name='tag_list' id='item'}
                                    <option value="{$item.id}"
                                            {foreach name="data.tag" id="item_tag"}
                                            {if $item.id==$item_tag.id}selected{/if}
                                            {/foreach}
                                    >
                                    {$item.name}
                                    </option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>
                        <script>
                            $('#tag').select2();
                        </script>


                        <div class="form-group row">
                            <label for="description" class="col-sm-2 col-form-label">简介</label>
                            <div class="col-sm-10 col-md-4 formInputDiv">
                                <input id="description" name="description" value="{$data.description|default=''}"
                                       placeholder="请输入简介" type="text" class="form-control fieldText">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="content" class="col-sm-2 col-form-label">内容</label>
                            <div class="col-sm-10">
                                <div id="contentEditor">{$data.content|raw|default='<p></p>'}</div>
                                <textarea id="content" name="content" style="display: none">{$data.content|raw|default=''}</textarea>
                            </div>
                        </div>
                        <script>
                            var E = E || window.wangEditor;
                            if (editor_content !== undefined) {
                                editor_content.destroy();
                            }
                            var editor_content = new E('#contentEditor');
                            editor_content.config.zIndex = 1000;
                            editor_content.config.uploadImgServer = '{:url("admin/file/editor")}';
                            editor_content.config.uploadFileName = 'file';
                            editor_content.create();
                            editor_content.config.onchange = function (newHtml) {
                                $('#content').val(newHtml);
                            };


                        </script>
                        <div class="form-group row">
                            <label for="is_top" class="col-sm-2 col-form-label">是否置顶</label>
                            <div class="col-sm-10 col-md-4 formInputDiv">
                                <input class="input-switch" id="is_top" value="1" {if(!isset($data)
                                       ||$data.is_top==1)}checked{/if} type="checkbox"/>
                                <input class="switch fieldSwitch" placeholder="是否置顶" name="is_top"
                                       value="{$data.is_top|default='0'}" hidden/>
                            </div>
                        </div>

                        <script>
                            $('#is_top').bootstrapSwitch({
                                onText: "是",
                                offText: "否",
                                onColor: "success",
                                offColor: "danger",
                                onSwitchChange: function (event, state) {
                                    $(event.target).closest('.bootstrap-switch').next().val(state ? '1' : '0').change();
                                }
                            });
                        </script>
                        <div class="form-group row">
                            <label for="is_hot" class="col-sm-2 col-form-label">是否热门</label>
                            <div class="col-sm-10 col-md-4 formInputDiv">
                                <input class="input-switch" id="is_hot" value="1" {if(!isset($data)
                                       ||$data.is_hot==1)}checked{/if} type="checkbox"/>
                                <input class="switch fieldSwitch" placeholder="是否热门" name="is_hot"
                                       value="{$data.is_hot|default='0'}" hidden/>
                            </div>
                        </div>

                        <script>
                            $('#is_hot').bootstrapSwitch({
                                onText: "是",
                                offText: "否",
                                onColor: "success",
                                offColor: "danger",
                                onSwitchChange: function (event, state) {
                                    $(event.target).closest('.bootstrap-switch').next().val(state ? '1' : '0').change();
                                }
                            });
                        </script>
                        <div class="form-group row">
                            <label for="img" class="col-sm-2 col-form-label">缩略图</label>
                            <div class="col-sm-10 col-md-4 formInputDiv">
                                <div class="input-group">
                                    <input id="img_file" name="img_file" type="file" class="file-loading "
                                           data-initial-preview="{if isset($data)}{$data.img}{/if}">
                                    <input name="img" id="img" value="{$data.img|default=''}" hidden placeholder="请上传图片"
                                           class="fieldImage">
                                    <script>
                                        initUploadImg('img');
                                    </script>
                                </div>
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="sort_number" class="col-sm-2 col-form-label">排序</label>
                            <div class="col-sm-10 col-md-4 formInputDiv">
                                <div class="input-group">
                                    <input id="sort_number" name="sort_number"
                                           value="{$data.sort_number|default='1000'}" placeholder="请输入排序" type="number"
                                           class="form-control fieldNumber">
                                </div>
                            </div>
                        </div>
                        <script>
                            $('#sort_number')
                                .bootstrapNumber({
                                    upClass: 'success',
                                    downClass: 'primary',
                                    center: true
                                });
                        </script>
                        <div class="form-group row">
                            <label for="view_count" class="col-sm-2 col-form-label">浏览数</label>
                            <div class="col-sm-10 col-md-4 formInputDiv">
                                <div class="input-group">
                                    <input id="view_count" name="view_count" value="{$data.view_count|default='0'}"
                                           placeholder="请输入浏览数" type="number" class="form-control fieldNumber">
                                </div>
                            </div>
                        </div>
                        <script>
                            $('#view_count')
                                .bootstrapNumber({
                                    upClass: 'success',
                                    downClass: 'primary',
                                    center: true
                                });
                        </script>

                    </div>
                    <!-- 表单底部 -->
                    <div class="card-footer">
                        <div class="row">
                            <div class="col-sm-2">
                            </div>

                            <div class="col-sm-10 col-md-4 formInputDiv">

                                <div class="btn-group">
                                    <button type="submit" class="btn btn-sm btn-primary dataFormSubmit">
                                        保 存
                                    </button>
                                </div>
                                <div class="btn-group">
                                    <button type="reset" class="btn btn-sm btn-default dataFormReset">
                                        重 置
                                    </button>
                                </div>

                                {if !isset($data)}
                                <div class="btn-group float-right createContinueDiv">
                                    <input type="checkbox" class="form-check-input" value="1" id="_create"
                                           name="_create" title="继续添加数据">
                                    <label class="form-check-label" for="_create">继续添加</label>
                                </div>
                                {/if}
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</section>
<script>
    /** 表单验证 **/
    $('#dataForm').validate({
        rules: {
            'name': {
                required: true,

            },
            'user_id': {
                required: true,

            },
            'article_category_id': {
                required: true,

            },
            'description': {
                required: true,

            },
            'content': {
                required: true,

            },
            'is_top': {
                required: true,

            },
            'is_hot': {
                required: true,

            },
            'img': {
                required: true,

            },
            'sort_number': {
                required: true,

            },
            'view_count': {
                required: true,

            },

        },
        messages: {
            'name': {
                required: "标题不能为空",

            },
            'user_id': {
                required: "发布人不能为空",

            },
            'article_category_id': {
                required: "所属分类不能为空",

            },
            'description': {
                required: "简介不能为空",

            },
            'content': {
                required: "内容不能为空",

            },
            'is_top': {
                required: "是否置顶不能为空",

            },
            'is_hot': {
                required: "是否热门不能为空",

            },
            'img': {
                required: "缩略图不能为空",

            },
            'sort_number': {
                required: "排序不能为空",

            },
            'view_count': {
                required: "浏览数不能为空",

            },

        }
    });
</script>
{/block}